package cn.dinkevin.xui;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 定义统一的错误码
 * Created by ChengPengFei on 2016/12/22 0022.
 */

public final class ErrorInfo {

    private static final String UNKNOWN_ERROR = "未知错误 (%d)";

    private static final Map<Integer,String> mErrorMessage = new HashMap<>();

    private ErrorInfo(){}


    /**
     * 注册错误信息 K-V 到本地消息表中
     * @param messages
     */
    public static void register(Map<Integer,String> messages){
        if(null == messages) return;
        Iterator<Integer> it = messages.keySet().iterator();
        while (it.hasNext()){
            Integer code = it.next();
            String msg = messages.get(code);
            mErrorMessage.put(code,msg);
        }
    }


    /**
     * 错误码转换
     * @param error 错误码
     * @return
     */
    public static String getMessage(int error){

        switch (error){
            case UPLOAD_FILE_TIME_OUT:
                return String.format("上传失败 (%d)", UPLOAD_FILE_TIME_OUT);

            case FILE_NOT_EXIT:
                return String.format("文件不存在 (%d)", FILE_NOT_EXIT);

            case CONNECT_ERROR:
            case UNKNOWN_HOST:
            case CONNECT_TIMEOUT:
                return String.format("请检查网络设置 (%d)",error);

            case JSON_PARSE_ERROR:
                return String.format("解析数据异常 (%d)",error);

            case WRITE_DATA_TO_FILE:
                return String.format("写入本地数据失败 (%d)",error);

            case DATA_ERROR_VIDEO_COMPRESSOR:
                return String.format("数据处理异常 (%d)",error);

            default:
                return String.format(UNKNOWN_ERROR,error);
        }
    }

    public static final int INVALID_TOKEN = 1101;              // TOKEN 无效

    public final static int UNKNOWN = -100;                         // 未知错误
    public final static int UPLOAD_FILE_TIME_OUT = -110;            // 上传文件超时
    public final static int FILE_NOT_EXIT = -111;                   // 文件不存在
    public final static int WRITE_DATA_TO_FILE = -112;               // 创建文件失败
    public final static int DATA_ERROR_VIDEO_COMPRESSOR = -114;      // 数据处理异常(视频压缩）
    public static final int CONNECT_ERROR = -200;               // 连接失败
    public static final int CONNECT_TIMEOUT = -201;             // 连接超时
    public static final int UNKNOWN_HOST= -202;                 // 未知服务器
    public static final int JSON_PARSE_ERROR = -203;            // 解析数据异常
}
